{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "import csv\n",
    "import jieba"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "file = 'debunking.csv'\n",
    "with open(file, 'r', encoding='utf-8', errors='ignore') as file:\n",
    "    f_csv = csv.reader(file)\n",
    "    rows = [row for row in f_csv]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "file = './task1/train.csv'\n",
    "with open(file, 'r', encoding='utf-8', errors='ignore') as file:\n",
    "    f_csv = csv.reader(file)\n",
    "    rows = [row for row in f_csv]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "38472\n"
     ]
    }
   ],
   "source": [
    "fileLen = len(rows)\n",
    "print(fileLen)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "done!\n"
     ]
    }
   ],
   "source": [
    "save_train = open('./task1/task1_trn', 'w', encoding='utf-8')\n",
    "for line_id in range(1, fileLen - 5000):\n",
    "    line = rows[line_id]\n",
    "    save_train.write(line[0]+' ||| '+ line[1] +' ||| '+ line[2])\n",
    "\n",
    "save_dev = open('./task1/task1_dev', 'w', encoding='utf-8')\n",
    "for line_id in range(fileLen - 5000, fileLen):\n",
    "    line = rows[line_id]\n",
    "    save_dev.write(line[0]+' ||| '+ line[1] +' ||| '+ line[2] + '\\n')\n",
    "print('done!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "＂/李/的/76/人/律师团/领队/、/法律/大学/副校长/张/爱国/教授/对/媒体/表示/，/李/天一/因/第一个/与/被害/女子/发生/性关系/，/不/构成/轮奸/罪/，/只是/以/判罚/较轻/的/强奸罪/批捕/，/这是/律师团/所有/成员/共同努力/的/结果/。/＂/果断/记下/律师/大名/，/犯了罪/找/爱国/，/没错/！/尤其/轮奸/后/。\n",
      "【/男子/捏造/“/周浦/滴滴/司机/砍死/乘客/”/谣言/被/刑拘/称/只是/觉得/好玩/】\n",
      "【/善良/的/大桥/】/金沙江/大桥/在/没车/行走/的/时候/自我/坍塌/，/这桥通/人性/，/未伤/一车/一人/，/太/善良/了/。/应该/当选/2013/年度/《/感动/中国/》/特别/致敬/者/。/（/by/夏骏/）\n",
      "幼儿园/都/发/通知/了/，/家长/们/注意/啦/：/现在/得/白血病/的/小孩/越来越/多/，/妇幼保健/院/提示/您/，/请/不要/给/宝宝/喝/爽歪歪/和/有/添加剂/的/牛奶/饮料/，/告诉/家里/有/小孩/的/朋友/，/旺仔/牛奶/、/可口可乐/、/爽歪歪/、/娃哈哈/AD/钙奶/、/未来/星/、/QQ/星/、/美汁/源果/粒奶/优/菠萝/味/的/。/都/含有/肉毒/杆菌/。/现在/紧急/召回/。/请/转发/扩散/！/！/！\n",
      "上海/4000/吨/垃圾/偷倒/苏州/太湖/西山/岛/(/图/)/（/分享/自/@/新浪/新闻/）\n"
     ]
    }
   ],
   "source": [
    "document_cut = [jieba.cut(line[1]) for line in rows]\n",
    "for document_id in range(1,6):\n",
    "    print('/'.join(document_cut[document_id]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "def processing(datasets):\n",
    "    dataText, dataLabel = [], []\n",
    "    for dataset in datasets:\n",
    "        texts, labels = [], []\n",
    "        with open(dataset, 'r', encoding='utf-8', errors='ignore') as file:\n",
    "            for line in file:\n",
    "                try:\n",
    "                    line = line.replace(\"\\n\", \"\").split(\" ||| \")\n",
    "                    texts += [\" \".join(jieba.cut(line[1].strip()))]\n",
    "                    labels += [line[2].strip()]\n",
    "                except:\n",
    "                    continue\n",
    "        dataText += [texts]\n",
    "        dataLabel += [labels]\n",
    "    return dataText, dataLabel        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "datasets = [\"./task1/task1_trn\", \"./task1/task1_dev\"]\n",
    "textprint, label = processing(datasets)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
